#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/wait.h>

int main(int argc, char const *argv[])
{
     printf("main process: %d\n",getpid());
     int i=100; //在辟出子进程之前的变量  子进程是否可以继承  ，是否可以改变？
     int pid= fork();

     while(1)
     {
             if(pid>0)
            {

                printf(" main  process: %d ,fork pid: %d  , parent process :%d \n",getpid(),pid,getppid());
                printf("main process: %d  ,i: %d\n",getpid(),i);
                i++;
                if(i>505)
                {   
                    printf("等待子进程结束....\n");
                    //waitpid(pid,NULL,WUNTRACED);  //阻塞等待

               

                    printf(" 主进程结束....\n");
                    break;
                }
                 //非阻塞监控
                 int over_status=0;
                 pid_t  pid_from_wait= waitpid(pid,&over_status,WNOHANG);
                 printf("pid_from_wait: %d  ,over_status : %d\n",pid_from_wait,over_status);

            }
            else 
            {   
                
                printf("child process: %d   ,fork pid: %d ,parent process :%d\n",getpid() ,pid,getppid());
                i++;
                printf("child process: %d  ,i: %d\n",getpid(),i);
                if(i>200)
                {
                    printf("子进程结束....\n");
                    break;
                }
            }

            sleep(2);
     }

    return 0;
}
